<script lang="ts" name="productCard" setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import type { FormRules } from "element-plus";
import { useProductCardApi } from '/@/api/main/productCard';
import { useProductApi } from '/@/api/main/product';
import { Local } from '/@/utils/storage';

//父级传递来的函数，用于回调
const emit = defineEmits(["reloadTable"]);
const productCardApi = useProductCardApi();
const ruleFormRef = ref();

const state = reactive({
	title: '',
	loading: false,
	showDialog: false,
	ruleForm: {} as any,
	stores: {},
	dropdownData: {} as any,
	isBatch: false,
});

// 自行添加其他规则
const rules = ref<FormRules>({
	barcode: [{ required: true, message: '请扫描实体卡二维码', trigger: 'blur', },],
});

// 页面加载时
onMounted(async () => {
});

// 打开弹窗
const openDialog = async (title: string) => {
	state.title = title;
	state.ruleForm = {};
	state.showDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	emit("reloadTable");
	state.showDialog = false;
};

const handleEnterKey = async () => {
	if (state.ruleForm.barcode && !state.isBatch) {
		await submit(true);
	}
};

// 提交
const submit = async (keepWindow: boolean) => {
	if (state.loading) {
		return;
	}
	ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
		if (isValid) {
			let values = state.ruleForm;
			values.isBatch = state.isBatch;
			state.loading = true;
			try {
				await productCardApi.fillIn(values).then((res: any) => {
					state.ruleForm.barcode = '';
					if (!keepWindow) {
						var result = res.data.result;
						var message = '提交成功，成功' + result.successCount + '条，失败' + result.failureCount + '条';
						if (result.missCodeList.length > 0) {
							message += '，可能漏扫描：' + result.missCodeList.join(',');
						}
						ElMessage.success(message);
						closeDialog();
					} else {
						ElMessage.success('提交成功');
					}
				});
			}
			finally {
				state.loading = false;
			}
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: "error",
			});
		}
	});
};

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>
<template>
	<div class="productCard-container">
		<el-dialog v-model="state.showDialog" :width="800" draggable :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<span>{{ state.title }}</span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules"
				v-loading.fullscreen.lock="state.loading">
				<el-row>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="二维码" prop="barcode">
							<el-input type="textarea" height="50" rows="10" v-model="state.ruleForm.barcode"
								placeholder="请扫描实体卡二维码，一行一个" clearable @keyup.enter.native="handleEnterKey()" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-checkbox v-model="state.isBatch" style="margin-right: 50px;">批量提交</el-checkbox>
					<el-button @click="() => state.showDialog = false">取 消</el-button>
					<el-button @click="() => submit(false)" type="primary" v-reclick="1000">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>